import { ORANGE } from "../../miniprogram_npm/@vant/weapp/common/color"

// pages/invoice/invoice.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchText: "",
    selectStartData: "",
    selectEndData: "",
    listData: <any>[],
    oraginListData: <any>[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getListData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  getListData() {
    let res = [
      {
        parkName: "理塘停车场0",
        price: "3.50",
        date: "2022年11月20日",
        orderId: "HE1145141919810"
      }, {
        parkName: "理塘停车场2",
        price: "3.50",
        date: "2022年4月20日",
        orderId: "HE1145141919810"
      }, {
        parkName: "理塘停车场3",
        price: "3.50",
        date: "2022年1月1日",
        orderId: "HE1145141919810"
      }, {
        parkName: "理塘停车场0",
        price: "3.50",
        date: "2021年12月20日",
        orderId: "HE1145141919810"
      }, {
        parkName: "理塘停车场4",
        price: "3.50",
        date: "2021年8月20日",
        orderId: "HE1145141919810"
      }, {
        parkName: "理塘停车场5",
        price: "3.50",
        date: "2020年2月20日",
        orderId: "HE1145141919810"
      }
    ]
    this.setData({ oraginListData: res })
    this.filterListByDate()
  },

  setSearchText(e: any) {
    this.setData({ searchText: e.detail.value })
    // console.log(this.data.searchText);
    if (e.detail.value.length>0){
      this.filterListByText(e.detail.value)
    }else {
      this.filterListByText()
    }
  },
  bindStartDateChange(e: any) {
    this.setData({ selectStartData: e.detail.value })
    // console.log(e.detail);
    /* 设置列表 */
    if (this.data.selectEndData.length > 0) {
      this.filterListByDate(e.detail.value, this.data.selectEndData)
    } else {
      this.filterListByDate(e.detail.value)
    }
  },
  bindEndDateChange(e: any) {
    this.setData({ selectEndData: e.detail.value })
    /* 设置列表 */
    if (this.data.selectStartData.length > 0) {
      this.filterListByDate(this.data.selectStartData, e.detail.value)
    } else {
      this.filterListByDate("", e.detail.value)
    }
  },
  rechooseTime() {
    this.setData({
      selectStartData: "",
      selectEndData: ""
    })
    /* 重新设置列表 */
    this.filterListByDate()
  },

  /* 滚动到底部的操作 */
  scrollToLower(e: any) {
    console.log(e);
  },

  /* 处理点击复制 */
  handelCopy(e: any) {
    console.log();

    wx.setClipboardData({
      data: e.currentTarget.dataset.orderId,
      success(res) {
        wx.getClipboardData({
          success(res) {
            wx.showToast({
              title: '复制成功',
              icon: 'success',
              duration: 700
            })
          }
        })
      }
    })
  },

  filterListByDate(start?: string, end?: string): void {
    /* 假设格式为  2022年5月20日 */
    let sy: number, sm: number, ey: number, em: number
    if (start && start.length > 0 && end && end?.length > 0) {
      sy = parseInt(start.slice(0, 4))
      sm = parseInt(start.slice(5))
      ey = parseInt(end.slice(0, 4))
      em = parseInt(end.slice(5))
      let year, month
      let res = this.data.oraginListData.filter((el: any) => {
        year = parseInt(el.date.slice(0, el.date.indexOf("年")))
        month = el.date.slice(el.date.indexOf("年") + 1, el.date.indexOf("月"))
        if (year >= sy && year <= ey) {
          if (month >= sm && month <= em) {
            return true
          }
        } 
        return false
      })
      this.setData({
        listData: res
      })
    } else if (start && start.length > 0) {
      sy = parseInt(start.slice(0, 4))
      sm = parseInt(start.slice(5))
      let year, month
      let res = this.data.oraginListData.filter((el: any) => {
        year = parseInt(el.date.slice(0, el.date.indexOf("年")))
        month = el.date.slice(el.date.indexOf("年") + 1, el.date.indexOf("月"))
        if (year >= sy && month >= sm) {
          return true
        }
        return false
      })
      this.setData({
        listData: res
      })
    } else if (end && end.length > 0) {
      ey = parseInt(end.slice(0, 4))
      em = parseInt(end.slice(5))
      let year, month
      let res = this.data.oraginListData.filter((el: any) => {
        year = parseInt(el.date.slice(0, el.date.indexOf("年")))
        month = el.date.slice(el.date.indexOf("年") + 1, el.date.indexOf("月"))
        if (year <= ey && month <= em) {
          return true
        }
        return false
      })
      this.setData({
        listData: res
      })
    } else {
      this.setData({
        listData: this.data.oraginListData
      })
    }
  },

  filterListByText(text?: string): void {
    if (text) {
      let res = this.data.oraginListData.filter((el: any) => {
        if (el.parkName.includes(text)) {
          return true
        } else {
          return false
        }
      })
      this.setData({
        listData: res
      })
    } else {
      this.setData({ listData: this.data.oraginListData })
    }
  }
})